package com.lv.service.dialog

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.util.component1
import androidx.core.util.component2
import com.github.gzuliyujiang.wheelpicker.widget.LinkageWheelLayout
import com.lv.common.imp.OnPositionTMore
import com.lv.common.utils.TimeUtil
import com.lv.common.utils.YgxUtilsKt
import com.lv.common.utils.showToast
import com.lv.common.widget.linkbar.ZFlowLayout
import com.lv.service.R
import com.lv.common.bean.CommonSelectedBean
import com.lv.service.room.category.CategoryBean
import com.lv.service.utils.YgxToolsKt
import com.lv.service.weidget.picker.ThirdLikeProvider
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 *         mTimeLearBeans.add(CommonSelectedBean("上上周", postiont = -4))
 *         mTimeLearBeans.add(CommonSelectedBean("上周", postiont = -3))
 *         mTimeLearBeans.add(CommonSelectedBean("本周", postiont = -2))
 *         mTimeLearBeans.add(CommonSelectedBean("今天", postiont = -1))
 *         mTimeLearBeans.add(CommonSelectedBean("近3月", postiont = 3))
 *         mTimeLearBeans.add(CommonSelectedBean("近半年", postiont = 6))
 *         mTimeLearBeans.add(CommonSelectedBean("近1年", postiont = 12))
 *         四种tab 时间选择器
 */
class Time4StepTabPopupView(
    private val mContext: Context,
    private val mOpenBeans: List<CommonSelectedBean>? = null,
    private var mStartTime: String = "",
    private var mEndTime: String = "",
    private var mWheelPos: Int? = 0,//显示第几个
    private val positionListener: OnPositionTMore<String>? = null
) :
    BottomPopupView(mContext), OnClickListener {

    private val mAllBeans = mutableListOf<CategoryBean>()
    private var mCusTopPosition: Int = -1
    private var mWheelDayPosition1: Int = 0
    private var mWheelDayPosition2: Int = 0
    private var mWheelDayPosition3: Int = 0
    private var mWheelMouthPosition1: Int = 0
    private var mWheelMouthPosition2: Int = 0
    private var mWheelYearPosition: Int = 0
    private var mWheelStartPosition1: Int = 0
    private var mWheelStartPosition2: Int = 0
    private var mWheelStartPosition3: Int = 0
    private var mWheelEndPosition1: Int = 0
    private var mWheelEndPosition2: Int = 0
    private var mWheelEndPosition3: Int = 0
    private var tvTabDay: TextView? = null
    private var tvTabMonth: TextView? = null
    private var tvTabYear: TextView? = null
    private var tvTitleCustom: TextView? = null
    private var tvStart: TextView? = null
    private var tvEnd: TextView? = null
    private var tvSure: TextView? = null
    private var tvTitleSub: TextView? = null
    private var mFlowLayout: ZFlowLayout? = null
    private var wheelLinkageDay: LinkageWheelLayout? = null
    private var wheelLinkageMouth: LinkageWheelLayout? = null
    private var wheelLinkageYear: LinkageWheelLayout? = null
    private var wheelLinkageCus: LinkageWheelLayout? = null
    private var clContainerTop: ConstraintLayout? = null
    private var ivClose: ImageView? = null
    private var tvLine1: View? = null
    private var tvLine2: View? = null
    private var tvLine3: View? = null
    private var tvLine4: View? = null
    private var viewStartLine: View? = null
    private var viewEndLine: View? = null
    private var viewTop: View? = null
    private var timeCurrent: Int = 0//自定义时间的点击位置，0 开始时间 1 结束时间
    private val mMonday = TimeUtil.getDayOfWeek(1, "yyyy-MM-dd")//本周一的日期
    private val mTodayday = TimeUtil.getNowDateTime("yyyy-MM-dd")//今天的日期
    private var mProMonday = ""//上周一的日期
    private var mProSunday = ""//上周日的日期
    private var mPro2Monday = ""//上上周一的日期
    private var mPro2Sunday = ""//上上周日的日期
    private var mStartDayTime = ""//日选择时的开始时间
    private var mEndDayTime = ""//日选择时的结束时间
    private var mStartMonthTime = ""//月选择时的开始时间
    private var mEndMonthTime = ""//月选择时的结束时间
    private var mStartYearTime = ""//年选择时的开始时间
    private var mEndYearTime = ""//年选择时的结束时间
    private var mStartCusTime = ""//自定义时间的开始时间
    private var mEndCusTime = ""//自定义时间的结束时间
    private var mMaxDistance = 0//自定义时间时，最大的时间跨度
    private val mCurrentMonday = TimeUtil.getDayOfWeek(1, "yyyy-MM-dd")//本周一的日期

    private var mYear = String.format("%02d", TimeUtil.getYear()) //当前年
    private var mMonth = String.format("%02d", TimeUtil.getMonth()) //当前月
    private var mDay = String.format("%02d", TimeUtil.getDay()) //当前日

    private val mViewList = mutableListOf<View>()

    override fun getImplLayoutId(): Int {
        return R.layout.time_4step_tab_popup
    }

    override fun onCreate() {
        super.onCreate()
        //上上周
        mPro2Monday = TimeUtil.getPreviousDaysDate(14, mMonday, "yyyy-MM-dd")
        mPro2Sunday = TimeUtil.getBeforDaysDate(6, mPro2Monday, "yyyy-MM-dd")

        //上周
        mProMonday = TimeUtil.getPreviousDaysDate(7, mMonday, "yyyy-MM-dd")
        mProSunday = TimeUtil.getBeforDaysDate(6, mProMonday, "yyyy-MM-dd")


        viewTop = findViewById<View>(R.id.view_top)
        tvTabDay = findViewById<TextView>(R.id.tv_tab_day)
        tvTabMonth = findViewById<TextView>(R.id.tv_tab_month)
        tvTabYear = findViewById<TextView>(R.id.tv_tab_year)
        tvTitleCustom = findViewById<TextView>(R.id.tv_title_custom)
        tvStart = findViewById<TextView>(R.id.tv_start)
        tvEnd = findViewById<TextView>(R.id.tv_end)
        tvSure = findViewById<TextView>(R.id.tv_sure)
        tvTitleSub = findViewById<TextView>(R.id.tv_title_sub)
        mFlowLayout = findViewById<ZFlowLayout>(R.id.z_flow)
        wheelLinkageDay = findViewById<LinkageWheelLayout>(R.id.wheel_linkage_day)
        wheelLinkageMouth = findViewById<LinkageWheelLayout>(R.id.wheel_linkage_mouth)
        wheelLinkageYear = findViewById<LinkageWheelLayout>(R.id.wheel_linkage_year)
        wheelLinkageCus = findViewById<LinkageWheelLayout>(R.id.wheel_linkage_cus)
        clContainerTop = findViewById<ConstraintLayout>(R.id.cl_container_top)
        ivClose = findViewById<ImageView>(R.id.iv_close)
        tvLine1 = findViewById<View>(R.id.tv_line1)
        tvLine2 = findViewById<View>(R.id.tv_line2)
        tvLine3 = findViewById<View>(R.id.tv_line3)
        tvLine4 = findViewById<View>(R.id.tv_line4)
        viewStartLine = findViewById<View>(R.id.view_start_line)
        viewEndLine = findViewById<View>(R.id.view_end_line)
        viewTop?.setOnClickListener(this)
        tvTabDay?.setOnClickListener(this)
        tvTabMonth?.setOnClickListener(this)
        tvTabYear?.setOnClickListener(this)
        tvTitleCustom?.setOnClickListener(this)
        tvSure?.setOnClickListener(this)
        ivClose?.setOnClickListener(this)
        tvStart?.setOnClickListener(this)
        tvEnd?.setOnClickListener(this)
        initView()
        initUI()
        initTimeData()
        initEvnet()
    }


    private fun initView() {
        initWheelsDatas()
        setFlowData()
    }

    private fun initWheelsDatas() {
        val year = TimeUtil.getYear()
        val month = TimeUtil.getMonth()
        val day = TimeUtil.getDay()
        val times = YgxToolsKt.initSelectedNoAllTime(year, month, day, showUnit = false)
        mAllBeans.clear()
        mAllBeans.addAll(times)
        val thirdLikeProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkageDay?.setData(thirdLikeProvider)
        val (x, y, z) = initPositionTimePicker(mTodayday)
        mWheelDayPosition1 = x
        mWheelDayPosition2 = y
        mWheelDayPosition3 = z
        wheelLinkageDay?.setDefaultValue(
            mWheelDayPosition1,
            mWheelDayPosition2,
            mWheelDayPosition3
        )
        if (mStartTime.isNotEmpty()) {
            mStartDayTime = mStartTime
            mEndDayTime = mStartTime
        } else {
            mStartDayTime = mTodayday
            mEndDayTime = mTodayday
        }

        val thirdLikeMouthProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkageMouth?.setData(thirdLikeMouthProvider)

        mWheelMouthPosition1 = x
        mWheelMouthPosition2 = y
        wheelLinkageMouth?.setDefaultValue(mWheelMouthPosition1, mWheelMouthPosition2, 0)
        wheelLinkageMouth?.thirdWheelView?.visibility = View.GONE

        val thirdLikeYearProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkageYear?.setData(thirdLikeYearProvider)
        mWheelYearPosition = x
        wheelLinkageYear?.setDefaultValue(mWheelYearPosition, 0, 0)
        wheelLinkageYear?.secondWheelView?.visibility = View.GONE
        wheelLinkageYear?.thirdWheelView?.visibility = View.GONE

        val thirdLikeCusProvider = ThirdLikeProvider(mAllBeans)
        wheelLinkageCus?.setData(thirdLikeCusProvider)
        mWheelStartPosition1 = x
        mWheelStartPosition2 = y
        mWheelStartPosition3 = z
        mWheelEndPosition1 = x
        mWheelEndPosition2 = y
        mWheelEndPosition3 = z

        wheelLinkageCus?.setDefaultValue(
            mWheelStartPosition1,
            mWheelStartPosition2,
            mWheelStartPosition3
        )

    }

    private fun setFlowData() {
        mViewList.clear()
        if (!mOpenBeans.isNullOrEmpty()) {
            for (i in 0 until mOpenBeans.size) {
                val textChild = LayoutInflater.from(mContext)
                    .inflate(
                        R.layout.text_tag_layout_gray,
                        mFlowLayout, false
                    ) as TextView
                textChild.text = mOpenBeans[i].name
                mViewList.add(textChild)
            }
            if (mCusTopPosition != -1) {
                mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
            }

            mFlowLayout?.setChildren(mViewList)
            mFlowLayout?.setOnTagClickListener { view, position ->
                mViewList.forEach { viewItem ->
                    viewItem.setBackgroundResource(com.lv.common.R.drawable.bg_f9_15)
                }
                mOpenBeans.forEach { viewItem ->
                    viewItem.selected = false
                }
                mCusTopPosition = position
                mOpenBeans[position].selected = true
                mViewList[position].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                val postiont = mOpenBeans[position].postiont
                when (postiont) {
                    -4 -> {//上上周
                        mWheelPos = 3
                        mStartCusTime = mPro2Monday
                        mEndCusTime = mPro2Sunday
                    }

                    -3 -> {//上周
                        mStartCusTime = mProMonday
                        mEndCusTime = mProSunday
                    }

                    -2 -> {//本周
                        mStartCusTime = mMonday
                        mEndCusTime = mTodayday
                    }

                    -1 -> {//今日
                        mStartCusTime = mTodayday
                        mEndCusTime = mTodayday
                    }

                    else -> {
                        val yearCurrent = TimeUtil.getYear()
                        val monthCurrent = TimeUtil.getMonth()
                        val dayCurrent = TimeUtil.getDay()
                        val (a, b) = TimeUtil.getPreData(
                            yearCurrent, monthCurrent, postiont
                        )
                        val list = mutableListOf<CategoryBean>()
                        mAllBeans.let { list.addAll(it) }
                        val (x, y, z) = YgxToolsKt.initThirdAddressInt(
                            list, "$a", String.format("%02d", b), mDay
                        )
                        mWheelStartPosition1 = x
                        mWheelStartPosition2 = y
                        val isExist = TimeUtil.isDateExists(a, b, dayCurrent)
                        if (!isExist) {
                            mWheelStartPosition3 =
                                (mAllBeans.get(mWheelStartPosition1).telSecondCategories?.get(
                                    mWheelStartPosition2
                                )?.telThirdCategories?.size ?: 1) - 1
                        } else {
                            mWheelStartPosition3 = z
                        }
                        mStartCusTime = getWheelTime(
                            mWheelStartPosition1,
                            mWheelStartPosition2,
                            mWheelStartPosition3
                        )
                        mEndCusTime = mTodayday
                        val (j, k, m) = initPositionTimePicker(mTodayday)
                        mWheelEndPosition1 = j
                        mWheelEndPosition2 = k
                        mWheelEndPosition3 = m
                    }
                }

                initStartScanTimePicker()
                initEndScanTimePicker()

                when (timeCurrent) {
                    0 -> {
                        wheelLinkageCus?.setDefaultValue(
                            mWheelStartPosition1,
                            mWheelStartPosition2,
                            mWheelStartPosition3
                        )

                        tvStart?.setTextColor(Color.parseColor("#eb613d"))
                        viewStartLine?.setBackgroundResource(com.lv.common.R.drawable.bg_theme_2)
                        tvEnd?.setTextColor(Color.parseColor("#242424"))
                        viewEndLine?.setBackgroundResource(com.lv.common.R.drawable.bg_24_2)
                    }

                    1 -> {
                        wheelLinkageCus?.setDefaultValue(
                            mWheelEndPosition1,
                            mWheelEndPosition2,
                            mWheelEndPosition3
                        )
                        tvStart?.setTextColor(Color.parseColor("#242424"))
                        viewStartLine?.setBackgroundResource(com.lv.common.R.drawable.bg_24_2)
                        tvEnd?.setTextColor(Color.parseColor("#eb613d"))
                        viewEndLine?.setBackgroundResource(com.lv.common.R.drawable.bg_theme_2)
                    }
                }
                tvStart?.text = mStartCusTime
                tvEnd?.text = mEndCusTime
//                mPosition4Listener?.onClick(position, -1, -1, -1)
//                dismiss()
            }
        }

    }

    private fun initUI() {
        when (mWheelPos) {// 0 日期选择 1  按月 2 按年 3 自定义时间
            0 -> {//日期选择
                wheelLinkageDay?.visibility = View.VISIBLE
                wheelLinkageMouth?.visibility = View.GONE
                wheelLinkageYear?.visibility = View.GONE
                wheelLinkageCus?.visibility = View.GONE
                clContainerTop?.visibility = View.GONE
                tvLine1?.visibility = View.VISIBLE
                tvLine2?.visibility = View.GONE
                tvLine3?.visibility = View.GONE
                tvLine4?.visibility = View.GONE
            }

            1 -> {//按月
                wheelLinkageDay?.visibility = View.GONE
                wheelLinkageMouth?.visibility = View.VISIBLE
                wheelLinkageYear?.visibility = View.GONE
                wheelLinkageCus?.visibility = View.GONE
                clContainerTop?.visibility = View.GONE
                tvLine1?.visibility = View.GONE
                tvLine2?.visibility = View.VISIBLE
                tvLine3?.visibility = View.GONE
                tvLine4?.visibility = View.GONE
            }

            2 -> {//按年
                wheelLinkageDay?.visibility = View.GONE
                wheelLinkageMouth?.visibility = View.GONE
                wheelLinkageYear?.visibility = View.VISIBLE
                wheelLinkageCus?.visibility = View.GONE
                clContainerTop?.visibility = View.GONE
                tvLine1?.visibility = View.GONE
                tvLine2?.visibility = View.GONE
                tvLine3?.visibility = View.VISIBLE
                tvLine4?.visibility = View.GONE
            }


            else -> {//自定义
                wheelLinkageDay?.visibility = View.GONE
                wheelLinkageMouth?.visibility = View.GONE
                wheelLinkageYear?.visibility = View.GONE
                wheelLinkageCus?.visibility = View.VISIBLE
                clContainerTop?.visibility = View.VISIBLE
                tvLine1?.visibility = View.GONE
                tvLine2?.visibility = View.GONE
                tvLine3?.visibility = View.GONE
                tvLine4?.visibility = View.VISIBLE
            }

        }
    }

    private fun initTimeData() {
        if (mStartTime.isEmpty()) {
            return
        }
        when (mWheelPos) {// 0 日期选择 1  按月 2 按年 3 自定义时间
            0 -> {//日期选择
                mStartDayTime = mStartTime
                mEndDayTime = mEndTime
                val (j, k, m) = initPositionTimePicker(mStartDayTime)
                mWheelDayPosition1 = j
                mWheelDayPosition2 = k
                mWheelDayPosition3 = m
                wheelLinkageDay?.setDefaultValue(
                    mWheelDayPosition1, mWheelDayPosition2, mWheelDayPosition3
                )
            }

            1 -> {//按月
                mStartMonthTime = mStartTime
                mEndMonthTime = mEndTime
                val (j, k, m) = initPositionTimePicker(mStartMonthTime)
                mWheelMouthPosition1 = j
                mWheelMouthPosition2 = k
                wheelLinkageMouth?.setDefaultValue(
                    mWheelMouthPosition1, mWheelMouthPosition2, 0
                )
            }

            2 -> {//按年
                mStartYearTime = mStartTime
                mEndYearTime = mEndTime
                val (j, k, m) = initPositionTimePicker(mStartYearTime)
                mWheelYearPosition = j
                wheelLinkageYear?.setDefaultValue(
                    mWheelYearPosition, 0, 0
                )
            }


            else -> {//自定义
                mStartCusTime = mStartTime
                mEndCusTime = mEndTime
                tvStart?.text = mStartCusTime
                tvEnd?.text = mEndCusTime
                val (j, k, m) = initPositionTimePicker(mStartTime)
                mWheelStartPosition1 = j
                mWheelStartPosition2 = k
                mWheelStartPosition3 = m
                val (a, b, c) = initPositionTimePicker(mEndCusTime)
                mWheelEndPosition1 = a
                mWheelEndPosition2 = b
                mWheelEndPosition3 = c
                wheelLinkageCus?.setDefaultValue(
                    mWheelStartPosition1,
                    mWheelStartPosition2,
                    mWheelStartPosition3
                )
                if (!mOpenBeans.isNullOrEmpty()) {

                    for (i in 0 until mOpenBeans.size) {
                        val postiont = mOpenBeans[i].postiont
                        when (postiont) {
                            -4 -> {//上上周
                                if (mStartCusTime == mPro2Monday && mEndCusTime == mPro2Sunday) {
                                    mOpenBeans.forEach { viewItem ->
                                        viewItem.selected = false
                                    }
                                    mCusTopPosition = i
                                    mOpenBeans[mCusTopPosition].selected = true
                                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                                    break
                                }
                            }

                            -3 -> {//上周
                                if (mStartCusTime == mProMonday && mEndCusTime == mProSunday) {
                                    mOpenBeans.forEach { viewItem ->
                                        viewItem.selected = false
                                    }
                                    mCusTopPosition = i
                                    mOpenBeans[mCusTopPosition].selected = true
                                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                                    break
                                }
                            }

                            -2 -> {//本周
                                if (mStartCusTime == mMonday && mEndCusTime == mTodayday) {
                                    mOpenBeans.forEach { viewItem ->
                                        viewItem.selected = false
                                    }
                                    mCusTopPosition = i
                                    mOpenBeans[mCusTopPosition].selected = true
                                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                                    break
                                }
                            }

                            -1 -> {//今日
                                if (mStartCusTime == mTodayday && mEndCusTime == mTodayday) {
                                    mOpenBeans.forEach { viewItem ->
                                        viewItem.selected = false
                                    }
                                    mCusTopPosition = i
                                    mOpenBeans[mCusTopPosition].selected = true
                                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                                    break
                                }
                            }

                            else -> {
                                val yearCurrent = TimeUtil.getYear()
                                val monthCurrent = TimeUtil.getMonth()
                                val (a, b) = TimeUtil.getPreData(
                                    yearCurrent, monthCurrent, postiont
                                )
                                val month = String.format("%02d", b) //当前月

                                val dataPro = "$a-$month-$mDay"
                                if (mEndCusTime == mTodayday && dataPro == mStartCusTime) {
                                    mOpenBeans.forEach { viewItem ->
                                        viewItem.selected = false
                                    }
                                    mCusTopPosition = i
                                    mOpenBeans[mCusTopPosition].selected = true
                                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                                    break
                                }
                            }
                        }
                    }
                }


                tvStart?.text = mStartCusTime
                tvEnd?.text = mEndCusTime
            }

        }

    }

    private fun initCustomTime() {
        when (timeCurrent) {
            0 -> {
                if (mStartCusTime.isEmpty()) {
//                    mWheelStartPosition1 = wheelLinkageCus?.firstWheelView?.currentPosition ?: 0
//                    mWheelStartPosition2 = wheelLinkageCus?.secondWheelView?.currentPosition ?: 0
//                    mWheelStartPosition3 = wheelLinkageCus?.thirdWheelView?.currentPosition ?: 0

                } else {
                    initStartScanTimePicker()
                }
                wheelLinkageCus?.setDefaultValue(
                    mWheelStartPosition1,
                    mWheelStartPosition2,
                    mWheelStartPosition3
                )
                mStartCusTime = getWheelTime(
                    mWheelStartPosition1,
                    mWheelStartPosition2,
                    mWheelStartPosition3
                )
                tvStart?.text = mStartCusTime
                tvEnd?.text = mEndCusTime
                tvStart?.setTextColor(Color.parseColor("#eb613d"))
                viewStartLine?.setBackgroundResource(com.lv.common.R.drawable.bg_theme_2)
                tvEnd?.setTextColor(Color.parseColor("#242424"))
                viewEndLine?.setBackgroundResource(com.lv.common.R.drawable.bg_24_2)
            }

            1 -> {
                if (mEndCusTime.isEmpty()) {
//                    mWheelEndPosition1 = wheelLinkageCus?.firstWheelView?.currentPosition ?: 0
//                    mWheelEndPosition2 = wheelLinkageCus?.secondWheelView?.currentPosition ?: 0
//                    mWheelEndPosition3 = wheelLinkageCus?.thirdWheelView?.currentPosition ?: 0

                } else {
                    initEndScanTimePicker()
                }
                wheelLinkageCus?.setDefaultValue(
                    mWheelEndPosition1,
                    mWheelEndPosition2,
                    mWheelEndPosition3
                )

                mEndCusTime = getWheelTime(
                    mWheelEndPosition1,
                    mWheelEndPosition2,
                    mWheelEndPosition3
                )
                tvStart?.text = mStartCusTime
                tvEnd?.text = mEndCusTime
                tvStart?.setTextColor(Color.parseColor("#242424"))
                viewStartLine?.setBackgroundResource(com.lv.common.R.drawable.bg_24_2)
                tvEnd?.setTextColor(Color.parseColor("#eb613d"))
                viewEndLine?.setBackgroundResource(com.lv.common.R.drawable.bg_theme_2)
            }
        }
    }

    private fun initStartScanTimePicker() {
        val list = mutableListOf<CategoryBean>()
        mAllBeans.let { list.addAll(it) }
        val startSplit = mStartCusTime.split("-")
        val (x, y, z) = YgxToolsKt.initThirdAddressInt(
            list,
            startSplit[0],
            String.format("%02d", startSplit[1].toDouble().toInt()),
            String.format("%02d", startSplit[2].toDouble().toInt())
        )
        mWheelStartPosition1 = x
        mWheelStartPosition2 = y
        mWheelStartPosition3 = z
    }

    private fun initEndScanTimePicker() {
        val list = mutableListOf<CategoryBean>()
        mAllBeans.let { list.addAll(it) }
        val endSplit = mEndCusTime.split("-")
        val (a, b, c) = YgxToolsKt.initThirdAddressInt(
            list,
            endSplit[0],
            String.format("%02d", endSplit[1].toDouble().toInt()),
            String.format("%02d", endSplit[2].toDouble().toInt())
        )
        mWheelEndPosition1 = a
        mWheelEndPosition2 = b
        mWheelEndPosition3 = c
    }

    private fun initEvnet() {
        wheelLinkageDay?.setOnLinkageSelectedListener { first, second, third ->
            mWheelDayPosition1 = wheelLinkageDay?.firstWheelView?.currentPosition ?: 0
            mWheelDayPosition2 = wheelLinkageDay?.secondWheelView?.currentPosition ?: 0
            mWheelDayPosition3 = wheelLinkageDay?.thirdWheelView?.currentPosition ?: 0
            val time = getWheelTime(mWheelDayPosition1, mWheelDayPosition2, mWheelDayPosition3)
            mStartDayTime = time
            mEndDayTime = time

        }
        wheelLinkageMouth?.setOnLinkageSelectedListener { first, second, third ->
            mWheelMouthPosition1 = wheelLinkageMouth?.firstWheelView?.currentPosition ?: 0
            mWheelMouthPosition2 = wheelLinkageMouth?.secondWheelView?.currentPosition ?: 0
            val time = getWheelTime(mWheelMouthPosition1, mWheelMouthPosition2, 0)
            mStartMonthTime = time
            mEndMonthTime = YgxUtilsKt.getLastDayForMonth(time)

        }
        wheelLinkageYear?.setOnLinkageSelectedListener { first, second, third ->
            mWheelYearPosition = wheelLinkageYear?.firstWheelView?.currentPosition ?: 0
            val time = getWheelTime(mWheelYearPosition, 0, 0)
            mStartYearTime = time

            mEndYearTime = YgxUtilsKt.getLastDayForYear(time)

        }

        wheelLinkageCus?.setOnLinkageSelectedListener { first, second, third ->
            val position1 = wheelLinkageCus?.firstWheelView?.currentPosition ?: 0
            val position2 = wheelLinkageCus?.secondWheelView?.currentPosition ?: 0
            val position3 = wheelLinkageCus?.thirdWheelView?.currentPosition ?: 0
            val time = getWheelTime(position1, position2, position3)
            when (timeCurrent) {
                0 -> {
                    mWheelStartPosition1 = position1
                    mWheelStartPosition2 = position2
                    mWheelStartPosition3 = position3
                    mStartCusTime = time
                    tvStart?.text = mStartCusTime
                }

                1 -> {
                    mWheelEndPosition1 = position1
                    mWheelEndPosition2 = position2
                    mWheelEndPosition3 = position3
                    mEndCusTime = time
                    tvEnd?.text = mEndCusTime
                }
            }
            initTagView()

//            mBinding.tvSearchCategory.text =
//                "${mAllBeans!![mWheelPosition1].name}-${mAllBeans[mWheelPosition1].telSecondCategories!![mWheelPosition2].name}"
        }

    }

    private fun getWheelTime(mWheelPos1: Int, mWheelPos2: Int, mWheelPos3: Int): String {
        val year = mAllBeans.get(mWheelPos1).name
        var month =
            mAllBeans.get(mWheelPos1).telSecondCategories?.get(mWheelPos2)?.name ?: "1"
        month = String.format("%02d", month.toDouble().toInt())
        var day =
            mAllBeans.get(mWheelPos1).telSecondCategories?.get(mWheelPos2)
                ?.telThirdCategories?.get(mWheelPos3)?.name ?: "1"
        day = String.format("%02d", day.toDouble().toInt())
        val time = "${year}-${month}-${day}"
        return time
    }

    private fun initTagView() {
        mViewList.forEach { viewItem ->
            viewItem.setBackgroundResource(com.lv.common.R.drawable.bg_f9_15)
        }
        mOpenBeans?.forEach { viewItem ->
            viewItem.selected = false
        }

        if (!mOpenBeans.isNullOrEmpty()) {
            if (mStartCusTime == mTodayday && mEndCusTime == mTodayday) {
                for (i in 0 until mOpenBeans.size) {
                    mOpenBeans[i].selected = false
                    if (mOpenBeans[i].postiont == -1) {
                        mCusTopPosition = i
                    }
                }
                if (mCusTopPosition != -1) {
                    mOpenBeans[mCusTopPosition].selected = true
                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                }
            } else if (mStartCusTime == mPro2Monday && mEndCusTime == mPro2Sunday) {//上上周
                for (i in 0 until mOpenBeans.size) {
                    mOpenBeans[i].selected = false
                    if (mOpenBeans[i].postiont == -4) {
                        mCusTopPosition = i
                    }
                }
                if (mCusTopPosition != -1) {
                    mOpenBeans[mCusTopPosition].selected = true
                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                }
            } else if (mStartCusTime == mProMonday && mEndCusTime == mProSunday) {//上周
                for (i in 0 until mOpenBeans.size) {
                    mOpenBeans[i].selected = false
                    if (mOpenBeans[i].postiont == -3) {
                        mCusTopPosition = i
                    }
                }
                if (mCusTopPosition != -1) {
                    mOpenBeans[mCusTopPosition].selected = true
                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                }
            } else if (mStartCusTime == mCurrentMonday && mEndCusTime == mTodayday) {//本周
                for (i in 0 until mOpenBeans.size) {
                    mOpenBeans[i].selected = false
                    if (mOpenBeans[i].postiont == -2) {
                        mCusTopPosition = i
                    }
                }
                if (mCusTopPosition != -1) {
                    mOpenBeans[mCusTopPosition].selected = true
                    mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                }
            } else {
                val yearCurrent = TimeUtil.getYear()
                val monthCurrent = TimeUtil.getMonth()

                for (i in 0 until mOpenBeans.size) {
                    mOpenBeans[i].selected = false
                    val postiont = mOpenBeans[i].postiont
                    val (a, b) = TimeUtil.getPreData(
                        yearCurrent, monthCurrent, postiont
                    )
                    val month = String.format("%02d", b) //月

                    val dataPro = "$a-$month-$mDay"
                    if (mEndCusTime == mTodayday && dataPro == mStartCusTime) {
                        mCusTopPosition = i
                        mOpenBeans[mCusTopPosition].selected = true
                        mViewList[mCusTopPosition].setBackgroundResource(com.lv.common.R.drawable.bg_tab_eb613d_14)
                        break
                    }
                }

            }
        }
//        LogUtils.e("mStartTime==${mStartTime}==mMonday==${mMonday}==mEndTime==${mEndTime}==mTodayday==${mTodayday}")

    }


    /**
     * @param time - 分割
     */
    private fun initPositionTimePicker(time: String): Triple<Int, Int, Int> {
        val list = mutableListOf<CategoryBean>()
        mAllBeans.let { list.addAll(it) }
        val startSplit = time.split("-")
        val (x, y, z) = YgxToolsKt.initThirdAddressInt(
            list,
            startSplit[0],
            String.format("%02d", startSplit[1].toDouble().toInt()),
            String.format("%02d", startSplit[2].toDouble().toInt())
        )
        return Triple(x, y, z)
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.view_top, R.id.iv_close -> {
                dismiss()
            }

            R.id.tv_tab_day -> {// 0 今天 1 本周 2 本月 3 24H 4 按月 5 按年 6 上上周 7 上周 8 本周 9 自定义时间
                if (mWheelPos != 0) {
                    mWheelPos = 0
//                    getWheelCurrentTime(0)
                    initUI()
                }
            }

            R.id.tv_tab_month -> {
                if (mWheelPos != 1) {
                    mWheelPos = 1
//                    getWheelCurrentTime(1)
                    initUI()
                }
            }

            R.id.tv_tab_year -> {
                if (mWheelPos != 2) {
                    mWheelPos = 2
//                    getWheelCurrentTime(2)
                    initUI()
                }
            }

            R.id.tv_title_custom -> {
                if (mWheelPos != 3) {
                    mWheelPos = 3
//                    mStartTime = mTodayday
//                    mEndTime = mTodayday
                    initUI()
                    initCustomTime()
                }
            }

            R.id.tv_start -> {
                if (timeCurrent != 0) {
                    timeCurrent = 0
                    initCustomTime()
                }
            }

            R.id.tv_end -> {
                if (timeCurrent != 1) {
                    timeCurrent = 1
                    initCustomTime()
                }
            }

            R.id.tv_sure -> {
                if (mMaxDistance > 0) {
                    val days = TimeUtil.getDaysBetweenDates(mStartTime, mEndTime, "yyyy-MM-dd")
                    if (days > (mMaxDistance - 1)) {
                        showToast("请选择${mMaxDistance}天内数据对比")
                        return
                    }
                }
                when (mWheelPos) {
                    0 -> {
                        mStartTime = mStartDayTime
                        mEndTime = mEndDayTime
                    }

                    1 -> {
                        mStartTime = mStartMonthTime
                        mEndTime = mEndMonthTime
                    }

                    2 -> {
                        mStartTime = mStartYearTime
                        mEndTime = mEndYearTime
                    }

                    3 -> {
                        if (mStartCusTime.isEmpty()) {
                            mStartCusTime = mTodayday
                            tvStart?.text = mTodayday
                        }
                        if (mEndCusTime.isEmpty()) {
                            mEndCusTime = mTodayday
                            tvEnd?.text = mTodayday
                        }
                        mStartTime = mStartCusTime
                        mEndTime = mEndCusTime
                        val result = YgxUtilsKt.compareDates(mStartCusTime, mEndCusTime)
                        when (result) {
                            1 -> {//第一个日期晚于第二个日期
                                val tempTime = mStartTime
                                mStartTime = mEndTime
                                mEndTime = tempTime
//                                    val po1 = mWheelStartPosition1
//                                    val po2 = mWheelStartPosition2
//                                    val po3 = mWheelStartPosition3
//                                    mWheelStartPosition1 = mWheelEndPosition1
//                                    mWheelStartPosition2 = mWheelEndPosition2
//                                    mWheelStartPosition3 = mWheelEndPosition3
//                                    mWheelEndPosition1 = po1
//                                    mWheelEndPosition2 = po2
//                                    mWheelEndPosition3 = po3
                                initTagView()
                            }

                            else -> {
                            }
                        }
                    }
                }


                positionListener?.onClick(
                    mWheelPos ?: 0, mStartTime, mEndTime
                )
                dismiss()
            }
        }
    }

    private fun getWheelCurrentTime(mWheelPos: Int) {
        when (mWheelPos) {
            0 -> {
                mWheelDayPosition1 = wheelLinkageDay?.firstWheelView?.currentPosition ?: 0
                mWheelDayPosition2 = wheelLinkageDay?.secondWheelView?.currentPosition ?: 0
                mWheelDayPosition3 = wheelLinkageDay?.thirdWheelView?.currentPosition ?: 0
                val time =
                    getWheelTime(mWheelDayPosition1, mWheelDayPosition2, mWheelDayPosition3)
                mStartTime = time
                mEndTime = time
            }

            1 -> {
                mWheelMouthPosition1 = wheelLinkageMouth?.firstWheelView?.currentPosition ?: 0
                mWheelMouthPosition2 = wheelLinkageMouth?.secondWheelView?.currentPosition ?: 0
                val time = getWheelTime(mWheelMouthPosition1, mWheelMouthPosition2, 0)
                mStartMonthTime = time
                mEndMonthTime = YgxUtilsKt.getLastDayForMonth(time)

            }

            2 -> {
                mWheelYearPosition = wheelLinkageYear?.firstWheelView?.currentPosition ?: 0
                val time = getWheelTime(mWheelYearPosition, 0, 0)
                mStartYearTime = time
                mEndYearTime = YgxUtilsKt.getLastDayForYear(time)
            }

//            3 -> {
//                mWheelStartPosition1 = wheelLinkageCus?.firstWheelView?.currentPosition ?: 0
//                mWheelStartPosition2 = wheelLinkageCus?.secondWheelView?.currentPosition ?: 0
//                mWheelStartPosition3 = wheelLinkageCus?.thirdWheelView?.currentPosition ?: 0
//                val time =
//                    getWheelTime(mWheelStartPosition1, mWheelStartPosition2, mWheelStartPosition3)
//                mStartCusTime = time
////                mEndTime = time
//            }

        }

    }

    fun setCusText(tabText: String) {
        tvTitleSub?.text = tabText
    }


    fun setTabText(tab: Int, tabText: String) {
        when (tab) {
            0 -> tvTabDay?.text = tabText
            1 -> tvTabMonth?.text = tabText
            2 -> tvTabYear?.text = tabText
            3 -> tvTitleCustom?.text = tabText
        }
    }

    fun setTabGone(tab: Int) {
        when (tab) {
            0 -> {
                tvTabDay?.visibility = View.GONE
                tvLine1?.visibility = View.GONE
            }

            1 -> {
                tvTabMonth?.visibility = View.GONE
                tvLine2?.visibility = View.GONE
            }

            2 -> {
                tvTabYear?.visibility = View.GONE
                tvLine3?.visibility = View.GONE
            }

            3 -> {
                tvTitleCustom?.visibility = View.GONE
                tvLine4?.visibility = View.GONE
            }
        }
    }


}